package com.platform.web.controller.system;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.system.service.ISysDictDataService;
import com.platform.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import com.platform.common.config.Global;
import com.platform.common.core.controller.BaseController;
import com.platform.framework.util.ShiroUtils;
import com.platform.system.domain.SysMenu;
import com.platform.system.domain.SysUser;
import com.platform.system.service.ISysConfigService;
import com.platform.system.service.ISysMenuService;

/**
 * 首页 业务处理
 * 
 * @author platform
 */
@Controller
public class SysIndexController extends BaseController
{
    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private RedisUtil redisUtil;

    // 系统首页
    @GetMapping("/index")
    public String index(ModelMap mmap)
    {
        JSONObject jsonObject = (JSONObject) redisUtil.get("observe");
        String menon = jsonObject.getString("001");
        String temp = jsonObject.getString("002")+"℃";
        String power = jsonObject.getString("003");
        String direction = jsonObject.getString("004");
        String damp = jsonObject.getString("005")+"%";
        String phenomenon = sysDictDataService.selectDictLabel("phenomenon", menon);
        power = sysDictDataService.selectDictLabel("power", power);
        direction = sysDictDataService.selectDictLabel("direction", direction);

        String message = " 气温："+temp+" 风力："+power+" 风向："+direction+" 湿度："+damp;

        mmap.put("phenomenon", "/platform/weather/"+menon+phenomenon+".png");

        mmap.put("message", message);

        int error = 0;

        if(redisUtil.hasKey("error")){
            error = Integer.parseInt(redisUtil.get("error").toString());
        }

        mmap.put("error", error);

        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        // 根据用户id取出菜单
        List<SysMenu> menus = menuService.selectMenusByUser(user);
        mmap.put("menus", menus);
        mmap.put("user", user);
        mmap.put("sideTheme", configService.selectConfigByKey("sys.index.sideTheme"));
        mmap.put("skinName", configService.selectConfigByKey("sys.index.skinName"));
        mmap.put("copyrightYear", Global.getCopyrightYear());
        return "index";
    }

    // 切换主题
    @GetMapping("/system/switchSkin")
    public String switchSkin(ModelMap mmap)
    {
        return "skin";
    }

    // 系统介绍
    @GetMapping("/system/main")
    public String main(ModelMap mmap)
    {
        mmap.put("version", Global.getVersion());
        return "main";
    }
}
